/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans.managed;

import beans.ejb.FantasyrosterperweekFacade;
import beans.ejb.NflplayergameFacade;
import beans.ejb.NflplayerseasonstatFacade;
import customEneities.Pair1;
import entities.Fantasyleague;
import entities.Fantasyrosterperweek;
import entities.Fantasyteam;
import entities.Fantasyuser;
import entities.Nflplayergame;
import entities.Nflplayerseasonstat;
import entities.Week;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author stumbledog
 */
@ManagedBean
@SessionScoped
public class MyTeam implements Serializable {
    @EJB
    private NflplayerseasonstatFacade nflplayerseasonstatFacade;
    @EJB
    private FantasyrosterperweekFacade fantasyrosterperweekFacade;
    @EJB
    private NflplayergameFacade nflplayergameFacade;
    
    private List<Fantasyrosterperweek> roster;
    private Fantasyleague currentLeague;
    private List<Fantasyteam> teamList;
    private Fantasyteam selectedTeam;
    private Week currentWeek;
    private List<Pair1> mergedList = new ArrayList<Pair1>();
    private Fantasyuser user;
    private HtmlDataTable table;

    public HtmlDataTable getTable() {
        return table;
    }

    public void selectTeam(){
        selectedTeam = (Fantasyteam) table.getRowData();
    }
    public void setTable(HtmlDataTable table) {
        this.table = table;
    }
    
    public List<Pair1> getMergedList() {
        mergedList.clear();
        roster = fantasyrosterperweekFacade.findByTeamByWeek(selectedTeam, currentWeek);
        Iterator<Fantasyrosterperweek> itr = roster.iterator();
        
        while(itr.hasNext()){
            Fantasyrosterperweek tmp = itr.next();
            Nflplayergame game = nflplayergameFacade.findByPlayerByWeek(tmp.getPlayerId(), currentWeek);
            mergedList.add(new Pair1(tmp,game));
        }
        return mergedList;
    }
    

    public void setMergedList(List<Pair1> mergedList) {
        this.mergedList = mergedList;
    }
    
    public MyTeam() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        AccountBean b = (AccountBean) request.getSession().getAttribute("accountBean");
        currentLeague = b.getCurrentLeague();
        currentWeek = b.getCurrentWeek();
        user = b.getAccount();
        System.out.println("myteam constructed!");
    }
    
    public Fantasyteam getSelectedTeam() {
        selectedTeam = (Fantasyteam) table.getRowData();
        return selectedTeam;
    }

    public void setSelectedTeam(Fantasyteam selectedTeam) {
        this.selectedTeam = selectedTeam;
    }
    public List<Fantasyteam> getTeamList() {
        teamList = (List)currentLeague.getFantasyteamCollection();
        return teamList;
    }

    public void setTeamList(List<Fantasyteam> teamList) {
        this.teamList = teamList;
    }    
}
